package com.psl.services.work;

import com.psl.Dao.Conn;
import com.psl.model.halfbringModel;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class halfbring {
    public List m_bring_list() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List zjkc = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT m.h_no, hp.h_name, m.h_size, m.h_r, m.h_n, m.h_bring_num, m.h_bring_datetime FROM halfproduct_list  hp, machinary_bring  m   WHERE m.h_no=hp.h_no  ORDER BY m.h_bring_datetime DESC");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfbringModel b = new halfbringModel();
                b.setH_no(rs.getString(1));
                b.setH_name(rs.getString(2));
                b.setH_size(rs.getString(3));
                b.setH_r(rs.getString(4));
                b.setH_n(rs.getString(5));
                b.setH_bring_num(rs.getInt(6));
                b.setH_bring_datetime(rs.getString(7));
                zjkc.add(b);
                HttpServletRequest request=ServletActionContext.getRequest();
                request.setAttribute("jjg",zjkc);
            }
            return zjkc;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List c_bring_list() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List zjkc = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT c.h_no, hp.h_name, c.h_size, c.h_r, c.h_n, c.h_bring_num, c.h_bring_datetime FROM halfproduct_list  hp, coating_bring  c WHERE c.h_no=hp.h_no  ORDER BY c.h_bring_datetime  DESC");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfbringModel b = new halfbringModel();
                b.setH_no(rs.getString(1));
                b.setH_name(rs.getString(2));
                b.setH_size(rs.getString(3));
                b.setH_r(rs.getString(4));
                b.setH_n(rs.getString(5));
                b.setH_bring_num(rs.getInt(6));
                b.setH_bring_datetime(rs.getString(7));
                zjkc.add(b);
                HttpServletRequest request=ServletActionContext.getRequest();
                request.setAttribute("zz",zjkc);

            }
            return zjkc;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean lookupkc(String h_no, String h_size, String h_r, int h_n, int h_bring_num, String h_bring_datetime, int position_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
//        System.out.println("services: h_no:"+h_no+"  h_size:"+h_size+"  h_r:"+h_r+"   h_n:"+h_n+" h_bring_num:"+h_bring_num+"  h_date:"+h_bring_datetime+" position_id"+position_id);
        try {
            pstmt = conn.prepareStatement("SELECT h_num_now from halfproduct_kc where h_no=? and h_size=? and h_r=? and h_n=?");
            pstmt.setString(1, h_no);
            pstmt.setString(2, h_size);
            pstmt.setString(3, h_r);
            pstmt.setInt(4, h_n);
            rs = pstmt.executeQuery();
            halfbringModel hbm;
            if (rs.next()) {
                hbm = new halfbringModel();
                hbm.setH_num_now(rs.getInt(1));
                int h_num_now = hbm.getH_num_now() - h_bring_num;
                HttpServletRequest request;
                if (h_num_now >= 0) {
                    UpdatejjgToSuccess(h_no, h_size, h_r, h_n, h_bring_num, h_bring_datetime, position_id);
//                    System.out.println("success");
                } else {
                    request = ServletActionContext.getRequest();
                    request.setAttribute("msg", "<script>alert('库存不足，不能出库')</script>");
//                    System.out.println("less");
                }
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean lookupzzpjkc(String h_no, String h_size, String h_r, int h_n, int h_bring_num, String h_bring_datetime, int position_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("SELECT h_num_now from halfproduct_kc where h_no=? and h_size=? and h_r=? and h_n=?");
            pstmt.setString(1, h_no);
            pstmt.setString(2, h_size);
            pstmt.setString(3, h_r);
            pstmt.setInt(4, h_n);
            rs = pstmt.executeQuery();
            halfbringModel hbm;
            if (rs.next()) {
                hbm = new halfbringModel();
                hbm.setH_num_now(rs.getInt(1));
                int h_num_now = hbm.getH_num_now() - h_bring_num;
                HttpServletRequest request;
                if (h_num_now >= 0) {
                    UpdatezzpjToSuccess(h_no, h_size, h_r, h_n, h_bring_num, h_bring_datetime, position_id);
                } else {
                    request = ServletActionContext.getRequest();
                    request.setAttribute("msg", "<script>alert('库存不足，不能出库')</script>");
                }
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean lookupjjgkc(String h_no, String h_size, String h_r, int h_n, int h_bring_num, String h_bring_datetime, int position_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("SELECT h_bring_num from machinary_bring where h_no=? and h_size=? and h_r=? and h_n=?");
            pstmt.setString(1, h_no);
            pstmt.setString(2, h_size);
            pstmt.setString(3, h_r);
            pstmt.setInt(4, h_n);
            rs = pstmt.executeQuery();
            halfbringModel hbm;
            if (rs.next()) {
                hbm = new halfbringModel();
                hbm.setH_bring_num(rs.getInt(1));
                int h_bringnum = hbm.getH_bring_num() - h_bring_num;
                HttpServletRequest request;
                if (h_bringnum >= 0) {
                    UpdatezzToSuccess(h_no, h_size, h_r, h_n, h_bring_num, h_bring_datetime, position_id);
                } else {
                    request = ServletActionContext.getRequest();
                    request.setAttribute("msg", "<script>alert('库存不足，不能出库')</script>");
                }
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


    public boolean UpdatejjgToSuccess(String h_no, String h_size, String h_r, int h_n, int h_bring_num, String h_bring_datetime, int position_id) {
        Connection conn = new Conn().getConn();
        CallableStatement cstmt = null;
        try {
            cstmt = conn.prepareCall("{call jjgpass(?,?,?,?,?,?,?)}");
            cstmt.setString(1, h_no);
            cstmt.setString(2, h_size);
            cstmt.setString(3, h_r);
            cstmt.setInt(4, h_n);
            cstmt.setInt(5, h_bring_num);
            cstmt.setString(6, h_bring_datetime);
            cstmt.setInt(7, position_id);
//            System.out.println("ccgc: h_no:"+h_no+"  h_size:"+h_size+"  h_r:"+h_r+"   h_n:"+h_n+" h_bring_num:"+h_bring_num+"  h_date:"+h_bring_datetime+" position_id"+position_id);
            cstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (cstmt != null) {
                    cstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean UpdatezzToSuccess(String h_no, String h_size, String h_r, int h_n, int h_id, String h_bring_datetime, int position_id) {
        Connection conn = new Conn().getConn();
        CallableStatement cstmt = null;
        try {
            cstmt = conn.prepareCall("{call zzpass(?,?,?,?,?,?,?)}");
            cstmt.setString(1, h_no);
            cstmt.setString(2, h_size);
            cstmt.setString(3, h_r);
            cstmt.setInt(4, h_n);
            cstmt.setInt(5, h_id);
            cstmt.setString(6, h_bring_datetime);
            cstmt.setInt(7, position_id);
            cstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (cstmt != null) {
                    cstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean UpdatezzpjToSuccess(String h_no, String h_size, String h_r, int h_n, int h_id, String h_bring_datetime, int position_id) {
        Connection conn = new Conn().getConn();
        CallableStatement cstmt = null;
        try {
            cstmt = conn.prepareCall("{call zzpjass(?,?,?,?,?,?,?)}");
            cstmt.setString(1, h_no);
            cstmt.setString(2, h_size);
            cstmt.setString(3, h_r);
            cstmt.setInt(4, h_n);
            cstmt.setInt(5, h_id);
            cstmt.setString(6, h_bring_datetime);
            cstmt.setInt(7, position_id);
            cstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (cstmt != null) {
                    cstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public List bring_log() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List zjkc = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT bl.h_no,hp.h_name,bl.h_size,bl.h_r,bl.h_n,bl.h_bring_num,bl.h_bring_datetime,bl.position_id FROM halfproduct_list hp,bring_log bl WHERE hp.h_no=bl.h_no  ORDER BY bl.h_bring_datetime DESC");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfbringModel b = new halfbringModel();
                b.setH_no(rs.getString(1));
                b.setH_name(rs.getString(2));
                b.setH_size(rs.getString(3));
                b.setH_r(rs.getString(4));
                b.setH_n(rs.getString(5));
                b.setH_bring_num(rs.getInt(6));
                b.setH_bring_datetime(rs.getString(7));
                b.setPosition_id(rs.getInt(8));
                zjkc.add(b);
                HttpServletRequest request=ServletActionContext.getRequest();
                request.setAttribute("xxxx",zjkc);
            }
            return zjkc;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
